<template>
  <div class="panel admin-panel">
    <div class="panel-head">
      <strong class="icon-reorder">企业列表</strong>
    </div>

    <table class="table table-hover text-center">
      <tr>
        <th>企业</th>
        <th>性质</th>
        <th>行业</th>
        <th>法人</th>
        <th>状态</th>
        <th>操作</th>
      </tr>
      <tr v-for="(u,index) in list.records" :key="u.id">
        <td>{{u.name}}</td>
        <td>{{u.nature}}</td>
        <td>{{u.industry}}</td>
        <td>{{u.legalPerson}}</td>
        <td>{{u.status|statusFilter}}</td>
        <td>
          <el-button
            v-if="u.status==1"
            type="warning"
            size="medium"
            @click="update(index,true)"
          >禁用</el-button>

          <el-button v-else type="success" size="medium" @click="update(index,false)">恢复</el-button>

        </td>
      </tr>
    </table>

    <!-- 分页 -->
    <div class="shop-page">
      <el-pagination
        background
        layout="total, prev, pager, next, jumper,sizes"
        :total="list.total"
        :current-page.sync="list.current"
        :page-count="list.pages"
        :page-size="list.size"
        :page-sizes="[3]"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "UserList",
  data() {
    return {
      list: {
        records: [
          { id: 1, account: "111", password: "123456", status: "1" },
          { id: 2, account: "222", password: "123456", status: "1" },
          { id: 3, account: "333", password: "123456", status: "1" }
        ],
        total: 10, // 总数
        current: 1, // 当前页
        pages: 4, // 总页数
        size: 3 // pageSize
      }
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    // 页码跳转
    handleCurrentChange(p) {
      this.getList(p);
    },
    // 获取页面
    getList(p) {
      axios.get(this.host + "/admin/company/list/" + p).then(r => {
        this.list = r.data.data;
      });
    },
    // 修改状态
    update(index, forbidden) {
      axios
        .get(
          this.host +
            "/admin/company/update/" +
            this.list.records[index].id +
            "/" +
            forbidden
        )
        .then(r => {
          if (r.data.code == "200") {
            this.suc(r.data.message);
            this.list.records[index].status = forbidden ? "2" : "1";
          } else {
            this.error(r.data.message);
          }
        });
    },
  },
  filters: {
    statusFilter(status) {
      if (status == "1") {
        return "正常";
      } else if (status == "2") {
        return "禁用";
      }
    }
  }
};
</script>

<style scoped>
</style>
